package com.example.administrator.szb.fragments.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.JGXQActivity;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.bean.IndexBean;
import com.example.administrator.szb.fragments.base.MVPBaseFragment;
import com.example.administrator.szb.fragments.fragment_forTab.home.adapter.JGadapter;
import com.example.administrator.szb.util.QTLog;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by stone on 2018/1/12.
 */

public class SearchJG extends MVPBaseFragment {
    View ll_nodata;
    ListView listView;
    private TextView top_tips;
    JGadapter adapter;

    ArrayList mdata_counListBean = new ArrayList<>();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_searchs,null);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void initView(View view) {
        top_tips = view.findViewById(R.id.top_tips);
        listView = view.findViewById(R.id.recyclerView);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(context,JGXQActivity.class);
                intent.putExtra("id",((IndexBean.DataBean.HotCounBean)mdata_counListBean.get(position)).getId());
                startActivity(intent);
            }
        });
        ll_nodata = view.findViewById(R.id.ll_nodata);
        isLoaded = true;
    }
    private boolean isLoaded = false;
    private boolean isfirst = true;
    @Override
    protected void initEvent() {
        if (!isLoaded){
            return;
        }
        if (mdata_counListBean.size()==0){
            if (!isfirst){
                listView.setVisibility(View.GONE);
                ll_nodata.setVisibility(View.VISIBLE);
            }


        }else {
            ll_nodata.setVisibility(View.GONE);
            listView.setVisibility(View.VISIBLE);
        }
        top_tips.setVisibility(View.VISIBLE);
        top_tips.setText("共搜索到"+mdata_counListBean.size()+"家相关机构");
        if (adapter==null){
            adapter = new JGadapter(context,mdata_counListBean);
            listView.setAdapter(adapter);
        }else {
            adapter.notifyDataSetChanged();
        }
        isfirst = false;

    }

    @Override
    protected void initData() {
        adapter = new JGadapter(context,mdata_counListBean);
        listView.setAdapter(adapter);
    }
    public void setDatas(List list){
        if (list==null){
            list = new ArrayList();
        }
        QTLog.e(""+list.size());
        mdata_counListBean.clear();
        mdata_counListBean.addAll(list);

        initEvent();
    }


}
